<template>
	<div class="view-new">
		<user-bar />

		<div class="body">
			<div class="banner"><img src="../../assets/imgs/bg.jpg" /></div>

			<create-insurance :onSubmit="onSubmitClick" />

		</div>

	</div>
</template>

<script>
import $http from '@/assets/http'
import loading from '@/components/loading'
import toast from '@/components/toast'
import alert from '@/components/alert'
import userBar from '@/components/userbar'
import createInsurance from '@/components/createInsurance'

export default {
	methods: {
		async onSubmitClick(data) {
			loading.show()
			try {
				const res = await this.fetchData(data)
				toast.show('提交成功')
				this.$router.push('/')
				loading.hide()
			} catch(e) {
                loading.hide()
                if (e.response.data.errorMsg) {
                    toast.show(e.response.data.errorMsg)
                } else {
                    alert.show('系统错误，请稍后再试')
                }
            }
		},
		fetchData(data) {
			return $http.request({
                method: 'post',
                url: '/ins/apply/add',
                data
            })
		}
	},
	components: {
		userBar,
		createInsurance
	}
}
</script>

<style scoped lang="scss">
	@import './style'
</style>